package uk.co.drpj.midi.router;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.ShortMessage;

public class ControllerHandle {

    String name;
    private int contrl;
    int minVal = 0;
    int maxVal = 127;
    int cmd;
    //private int zeroValue=0;

    public ControllerHandle(String name, int min, int max, int cntrl, int cmd) {
        this.name = name;
        minVal = min;
        maxVal = max;
        contrl = cntrl;
        this.cmd = cmd;
    }

    @Override
    public String toString() {
        return name;
    }

    public String getName() {

        return name;
    }

    public boolean isVelocity() {
        return cmd == ShortMessage.NOTE_ON;

    }

    public ShortMessage createMessage(int channel, float val) throws InvalidMidiDataException {
        ShortMessage mess = new ShortMessage();

        if (cmd == ShortMessage.CONTROL_CHANGE) {
            mess.setMessage(cmd, channel, contrl, (int) (val * 127.99f));
        } else if (cmd == ShortMessage.PITCH_BEND) {
            //    System.out.println(" Ptich bend controller not implemented yet ");


         //   ShortMessage shm = new ShortMessage();

            int ival = (int) (val * 8191.9f);
            mess.setMessage(ShortMessage.PITCH_BEND, channel, ival & 0x3f, (ival >> 7));  //  mess.setMessage(cmd, channel, contrl,(int) (val * 128.0f));

        } else {
            System.out.println(" Controller not implemented yet ");
        }

        return mess;
    }


    public int getController() { // Jens
        return contrl;
    }
}
